package
{
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import org.flixel.*;
	
	public class FriendBar extends FlxGroup
	{
		private static const NUM_LISTINGS:int = 6;
		private static const BAR_WIDTH:int = 700;
		private static const BAR_HEIGHT:int = 140;
		
		// thingies
		private var container:FlxSprite;
		private var listings:FlxGroup;
		
		private var friendNames:Array;
		private var friendIDs:Array;
		private var friendImages:Array;
		
		private var currentPage:int;
		private var leftArrow:FlxSprite;
		private var rightArrow:FlxSprite;
		
		private var startX:Number;
		private var startY:Number;
		
		private var noFriends:FlxGroup;
		private var invites:FlxText;
		
		private var initialized:Boolean;
		
		public function FriendBar(x:Number, y:Number)
		{
			startX = x;
			startY = y;
			initialized = false;
			currentPage = 1;
		}
		
		public function isInitialized():Boolean {
			return initialized;
		}
		
		public function initialize():void {
			initialized = true;
			initLayout();
			generateListings();
		}
		
		public function initLayout():void {
			container = new FlxSprite(startX, startY, Res.Friendbar);
			add(container);
			
			createArrows();
			
			listings = new FlxGroup();
			add(listings);
		}
		
		/**
		 * Create flxsprites for the arrows (but doesn't add them to page)
		 */
		private function createArrows():void {
			leftArrow = new FlxSprite(0, 0, Res.ArrowLeft);
			leftArrow.x = container.x + 5;
			leftArrow.y = container.y + (container.height - leftArrow.height)/2;
			
			rightArrow = new FlxSprite(0, 0, Res.ArrowRight);
			rightArrow.x = container.x + container.width - rightArrow.width - 5;
			rightArrow.y = leftArrow.y;
		}

		
		public function generateListings():void {
			var startIndex:int = (currentPage - 1) * NUM_LISTINGS;
			var friends:Array = Elemental.fbInfo.friends;
			
			if (friends.length == 0) {
				var noFriendsText:FlxText = new FlxText(container.x, container.y, container.width, Elemental.fbInfo.fullName + ", you have no friends playing Poofed.", false);
				noFriendsText.setFormat("Courier New", 20, 0xFFFFFF, "center");
				noFriendsText.y = container.y + (container.height - noFriendsText.height)/2;
				invites = new FlxText(container.x, container.y, container.width, "\nGo invite some buddies!", false);
				invites.setFormat("Courier New", 20, 0xFFFFFF, "center");
				invites.y = 2+container.y + (container.height - invites.height)/2;				
				
				noFriends = new FlxGroup();
				noFriends.add(noFriendsText);
				noFriends.add(invites);
				add(noFriends);
			} else {
				for (var i:int = 0; i < NUM_LISTINGS; i++) {
					var xPos:int = container.x + leftArrow.width + (FriendListing.WIDTH * i) + ((i+1) * 15);
					var yPos:int = container.y + (container.height - FriendListing.HEIGHT)/2 + 3;
					
					var listing:FriendListing;
					
					if (startIndex + i >= friends.length) {
						listing = new FriendListing(FriendListing.BLANK, xPos, yPos);
					} else {
						listing = new FriendListing(friends[startIndex + i]["id"], xPos, yPos);
					}
	
					listings.add(listing);
				}
				
				var totalPages:int = Math.ceil(friends.length / NUM_LISTINGS);
				updateArrows(currentPage, totalPages);
			}
		}
		
		/**
		 * Draws page arrows back on screen as necessary
		 */
		private function updateArrows(currPage:int, totalPages:int):void {
			if (currPage > 1) add(leftArrow);
			if (currPage < totalPages) add(rightArrow);
		}
		
		/**
		 * Called when the page arrows are clicked.
		 * Clears out listings and generates the next page's worth.
		 */
		private function requestNewPage(requestedPage:int):void {
			var totalPages:int = Math.ceil(Elemental.fbInfo.friends.length / NUM_LISTINGS);
			if (requestedPage < 1 || requestedPage > totalPages) {
				return;
			}
			
			currentPage = requestedPage;
			listings.clear();
			remove(leftArrow);
			remove(rightArrow);
			
			generateListings();
		}
		
		/**
		 * Listens for arrow clicks
		 */
		public override function update():void {
			super.update();
			if (invites != null) {
				if (invites.overlapsPoint(FlxG.mouse.x,FlxG.mouse.y)){
					invites.setFormat("Courier New", 20, 0x7A0023, "center");
					if (FlxG.mouse.justPressed()){
						Elemental.Log.logInvites();
						
						var myDelay:Timer = new Timer(1000);
						myDelay.addEventListener(TimerEvent.TIMER, function f():void{
							trace("sent invite request!");
							Elemental.fbInfo.sentRequest(Elemental.fbInfo.fullName+" invites you to play Poofed! Join now to play and create puzzles, and challenge your friends to play them!");
						});
						myDelay.start();
						
					}
				} else{
					invites.setFormat("Courier New", 20, 0xFFFFFF, "center");
				}
			}

			if (FlxG.mouse.justPressed()) {
				if (leftArrow.overlapsPoint(FlxG.mouse.screenX, FlxG.mouse.screenY)) {
					requestNewPage(currentPage - 1);
				}
				
				if (rightArrow.overlapsPoint(FlxG.mouse.screenX, FlxG.mouse.screenY)) {
					requestNewPage(currentPage + 1);
				}
			}
		}
		
		
		public function finishedLoading():Boolean {
			for (var i:int = 0; i < listings.length; i++) {
				var listing:FriendListing = listings.members[i];
				if (!listing.isFullyLoaded()) {
					return false;
				}
			}
			return true;
		}
		
	}
}